<template>
  <div id="userLoginPage">
    <h2 class="title">光点书集</h2>
    <div class="desc">光点书集，屏上寻知</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码长度不能少于8位' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item
        label="验证码"
        name="verifyCode"
        :rules="[{ required: true, message: '请输入验证码' }]"
      >
        <div class="captcha-container">
          <a-input
            v-model:value="formState.verifyCode"
            placeholder="请输入验证码"
            style="width: 50%"
          />
          <div class="captcha-image-container">
            <img :src="captchaImage" alt="验证码" class="captcha-image" @click="refreshCaptcha" />
            <span type="text" @click="refreshCaptcha" class="refresh-button"> 刷新 </span>
          </div>
        </div>
      </a-form-item>
      <div class="tips">
        没有账号?
        <router-link to="/user/register">去注册</router-link>
      </div>
      <a-form-item :wrapper-col="{ offset: 4 }">
        <a-button type="primary" html-type="submit" style="width: 100%; margin-top: 20px"
          >登录</a-button
        >
      </a-form-item>
    </a-form>
    <div class="test">
      上述是管理员账号
      <p>普通用户</p>
      <br />
      <p>账号：dousbzc</p>
      <br />
      <p>密码：12345678</p>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { verifyCodeUsingGet } from '@/api/kaptchaController'
import { userLoginUsingPost } from '@/api/userController'
import router from '@/router'
import { useLoginUserStore } from '@/stores/user'
import { message } from 'ant-design-vue'
import { onMounted, reactive, ref } from 'vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: 'dousb',
  userPassword: '12345678',
  verifyCode: '',
  captchaKey: '',
})
const captchaImage = ref('')
const loginUserStore = useLoginUserStore()
const ipAddress = ref('')
const cityName = ref('')
const weatherInfo = ref('')

// 获取验证码
const refreshCaptcha = async () => {
  try {
    const res = await verifyCodeUsingGet()
    if (res.data.code === 0 && res.data.data) {
      captchaImage.value = res.data.data.captchaImage
      formState.captchaKey = res.data.data.captchaKey
      formState.verifyCode = ''
    } else {
      message.error('获取验证码失败')
    }
  } catch (e: any) {
    message.error(e.message)
  }
}

// 提交表单
const handleSubmit = async (values: any) => {
  const loginData = {
    ...values,
    captchaKey: formState.captchaKey,
  }
  const res = await userLoginUsingPost(loginData)
  console.log('Success:', res)
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败，' + res.data.message)
    if (res.data.message?.includes('验证码')) {
      refreshCaptcha()
    }
  }
}

onMounted(() => {
  refreshCaptcha()
})
</script>

<style lang="scss" scoped>
#userLoginPage {
  max-width: 380px;
  margin: 0 auto;
  .title {
    text-align: center;
    font-size: 24px;
    margin-bottom: 16px;
    font-weight: bold;
  }
  .desc {
    text-align: center;
    font-size: 14px;
    color: #888;
    margin-bottom: 32px;
  }
  .tips {
    font-size: 13px;
    color: #888;
    text-align: right;
  }
  .captcha-container {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .captcha-image-container {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .captcha-image {
        width: 110px;
        height: 40px;
        object-fit: cover;
        cursor: pointer;
        transition: all 0.3s ease-in-out;
        margin-left: 8px;
      }
      .refresh-button {
        margin-left: 8px;
        cursor: pointer;
        transition: all 0.3s ease-in-out;
        color: #888;
        &:hover {
          color: #1890ff;
        }
      }
    }
  }
}
</style>
